|
|
Digital Filtering - A Fundamental Pre-Processing Step |
| Tags | pre-process☁filter |
The acquired electrophysiological signals have always two intrinsic components. The signal we really want to acquire/study and noise, i.e. the acquisition component that is not relevant for the study purposes.
Noise can have different origins, such as in random events or due to voluntary/involuntary movements of the subject under analysis that affect the signal acquisition
.
So, filtering
is a fundamental step that needs to be applied to the signal, in order to ensure the maximisation of signal to noise ratio
. Filtering can be achieved by hardware, having the analogical systems a great relevance, or by software using digital filters.
In this Jupyter Notebook it will be demonstrated how to digital filter the signal.
1 - Importation of the needed packages
# biosignalsnotebooks own package for loading and plotting the acquired data
import biosignalsnotebooks as bsnb
# Scientific package
import numpy
# Signal Processing package where digital filtering functions are included
from novainstrumentation.tools import plotfft
from novainstrumentation.filter import bandpass
2 - Load of acquired ECG data
# Load of data
data, header = bsnb.load_signal("ecg_4000_Hz", get_header=True)
In the following cell, some relevant information is stored inside variables. This relevant information includes the mac-address of the device, channel number and signal acquisition parameters such as resolution and sampling rate.
For a detailed explanation of how to access this info, the
"Signal Loading - Working with File Header"
Notebook should be consulted.
mac = "00:07:80:D8:A7:F9" # Mac-address
ch = "CH1" # Channel
sr = header[mac]["sampling rate"] # Sampling rate
resolution = header[mac]["resolution"] # Resolution (number of available bits)
3 - Generation of signal power spectrum by Fast Fourier Transform (FFT)
With this step is possible to observe the frequency composition of ECG signal.# Acquired data
signal = data[mac][ch]
# Power spectrum
freq_axis_1, power_spect_1 = plotfft(signal, sr)
4 - The informational content of ECG signal is typically contained inside the frequency band [0.5, 40] Hz
With the next representation we can conclude that exist some unwanted information out of this frequency band.5 - Application of a pass-band filter in order to be excluded the unwanted information out of [0.5, 40] Hz frequency band
# Digital filtering with a low cutoff frequency f1 of 0.5 Hz and high cutoff frequency f2 of 40 Hz
filter_signal_1 = bandpass(signal, f1=0.5, f2=40, order=1, fs=sr, use_filtfilt=True)
# Power spectrum
freq_axis_2, power_spect_2 = plotfft(filter_signal_1, sr)
6 - Comparison of the power spectrum of original and filtered signal
In this first filtering attempt we used a first order filter (input argument order=1). It can be seen, in the previous figure, that some unwanted information have been removed, unfortunately no filter has an ideal behavior, so despite we specify a high cutoff frequency of 40 Hz, some information above this threshold is maintained after filtering.
The good news are that components greater than 80 Hz are almost completely removed.
The filter performance can be improved by increasing the filter order, because the higher the filter order is, more quickly the transition between the pass and stop band will be. The transition band will be smaller because of a higher attenuation rate (-20 x order dB/decade).
However, the filter order must be chosen with precaution in order to avoid system instability.